package com.bw.tig.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.common.ResponseResult;
import com.bw.tig.entity.DTigether;
import com.bw.tig.entity.DTigetherReq;
import com.bw.tig.entity.DUser;
import com.bw.tig.entity.cvEntity;
import com.bw.tig.service.IDDetailService;
import com.bw.tig.service.IDTigetherService;
import com.bw.tig.service.IDUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xingjinning
 * @since 2020-06-10
 */
@RestController
@RequestMapping("/tigether")
public class DTigetherController {

    @Autowired
    private IDTigetherService tigetherService;
    @Autowired
    private IDUserService userService;
    @Autowired
    private IDDetailService detailService;

    @PostMapping("/list/{uid}")
    public ResponseResult list(@PathVariable("uid")long uid, @RequestBody DTigether dTigether){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            DUser user = userService.getById(uid);
            QueryWrapper queryWrapper = new QueryWrapper();
            if(dTigether.getAname()!=null){
                queryWrapper.like("tname",dTigether.getAname());
                queryWrapper.or();
                queryWrapper.like("aname",dTigether.getAname());
                queryWrapper.or();
                queryWrapper.like("bname",dTigether.getAname());
            }

            if(user.getRole()==0){
                queryWrapper.eq("uid",uid);
            }

            queryWrapper.orderByDesc("tigtime");
            List list = tigetherService.list(queryWrapper);
            result.setResult(list);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage(e.toString());
        }
        return result;
    }
    @PostMapping("/saveCv")
    public ResponseResult saveCv(@RequestBody cvEntity entitys){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            entitys.getTigForms().setStatus(0);
            tigetherService.insertCv(entitys);
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage(e.toString());
        }
        return result;
    }
    @PostMapping("/getTig/{tid}")
    public cvEntity getTig(@PathVariable("tid")long tid){
        cvEntity cvEntity = new cvEntity();
        DTigether tigether = tigetherService.getById(tid);
        cvEntity.setTigForms(tigether);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("tid",tid);
        List list = detailService.list(queryWrapper);
        cvEntity.setGoodss(list);
        return cvEntity;
    }
    @PostMapping("/subResume/{tid}")
    public ResponseResult subResume(@PathVariable("tid")long tid){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            DTigether tigether = tigetherService.getById(tid);
            tigether.setStatus(1);
            tigetherService.saveOrUpdate(tigether);
            result.setMessage("提交成功");
        }catch (Exception e){
            result.setMessage("提交失败"+e.toString());
            result.setSuccess(false);
        }
        return result;
    }
    @PostMapping("/NopassTig/{tid}")
    public ResponseResult NopassTig(@PathVariable("tid")long tid){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            DTigether tigether = tigetherService.getById(tid);
            tigether.setStatus(3);
            tigetherService.saveOrUpdate(tigether);
            result.setMessage("提交成功");
        }catch (Exception e){
            result.setMessage("提交失败"+e.toString());
            result.setSuccess(false);
        }
        return result;
    }
    @PostMapping("/passTig/{tid}")
    public ResponseResult passTig(@PathVariable("tid")long tid){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            DTigether tigether = tigetherService.getById(tid);
            tigether.setStatus(2);
            tigetherService.saveOrUpdate(tigether);
            result.setMessage("提交成功");
        }catch (Exception e){
            result.setMessage("提交失败"+e.toString());
            result.setSuccess(false);
        }
        return result;
    }
    @GetMapping("/getMoney")
    public ResponseResult getMoney(){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            DTigetherReq dTigetherReq=tigetherService.getMoney();
            result.setResult(dTigetherReq);
            result.setMessage("提交成功");
        }catch (Exception e){
            result.setMessage("提交失败"+e.toString());
            result.setSuccess(false);
        }
        return result;
    }
}
